package com.useoften;

public class TestString2 {
    public static void main(String[] args){
        String str8 = "";

        long num1 = Runtime.getRuntime().freeMemory();//获得系统剩余内存空间
        long time1 = System.currentTimeMillis();//获取系统的当前时间
        for (int i=0;i<5000;i++){
            str8 = str8 + i;
        }
        long num2 = Runtime.getRuntime().freeMemory();
        long time2 = System.currentTimeMillis();
        System.out.println("String占用内存：" + (num1 - num2));
        System.out.println("String占用时间：" + (time2 - time1));

        StringBuilder sb1 = new StringBuilder();
        long num3 = Runtime.getRuntime().freeMemory();
        long time3 = System.currentTimeMillis();

        /*使用StringBulider进行字符串的拼接*/
        for (int i = 0;i<5000;i++){
            sb1.append(i);
        }
        long num4 = Runtime.getRuntime().freeMemory();
        long time4 = System.currentTimeMillis();
        System.out.println("StringBuilder占用内存：" + (num3 - num4));
        System.out.println("StringBuilder占用时间：" + (time4 - time3));
    }
}
